package ink.lovejinhu.swardToOffer;

import ink.lovejinhu.common.ListNode;

import java.util.Stack;

/**
 * @author jinhu
 * created 2021-08-09 8:29
 */
public class Offer_006 {
    /**
     * 从尾到头打印链表
     * 这两个函数用错了
     * int[] res = new int[listNodes.size()];
     * // int[] res = new int[listNodes.capacity()];
     */
    public int[] reversePrint(ListNode head) {
        Stack<ListNode> listNodes = new Stack<>();

        while (head != null) {
            listNodes.push(head);
            head = head.next;
        }
        int[] res = new int[listNodes.size()];
        // int[] res = new int[listNodes.capacity()];
        int i = 0;
        while (!listNodes.isEmpty()) {
            res[i] = listNodes.pop().val;
            ++i;
        }
        return res;
    }
}
